'use strict';

angular.module('BC').controller('PendingBusinessController', ['$scope', '$resource', 'BaseService', 'BaseModule', 'BaseEnum', 'BCEnum', '$location', 'Authentication','BaseFunction',
    function($scope, $resource, BaseService, BaseModule, BaseEnum, BCEnum, $location, Authentication, BaseFunction) {
        $scope.CreateErpDocTypeEnum = BaseFunction.getEnum('BCEnum.CreateErpDocType');
        $scope.DirectionEnum = BaseFunction.getEnum('BCEnum.Direction');
        $scope.OperTypeEnum = BaseFunction.getEnum('BCEnum.OperType');


        $scope.All = false;
        $scope.IfCreatedERPDoc = false;
        $scope.NotCreatedERPDoc = true;

        BaseModule.initModule($scope, 'PendingBusiness');
        $scope.Module.PendingBusiness.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.PendingBusiness.Condition = { IsCreatedERPDoc:0 };
        $scope.Module.PendingBusiness.PageSize = 15;
        $scope.Module.PendingBusiness.Populate = [{
            path: 'RcvUser',
            select: 'Name Department Code',
            model: 'User',
            populate: {
                path: 'Department',
                select: 'Name',
                model: 'Department'
            }
        }];

        BaseModule.initModule($scope, 'PendingBusinessDetail');
        $scope.Module.PendingBusinessDetail.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.PendingBusinessDetail.PageSize = 15;

        //全部记录
        $scope.LoadAll = function() {
            if ($scope.All) {
                $scope.IfCreatedERPDoc = false;
                $scope.NotCreatedERPDoc = false;
                LoadData();
            }
            else{
            }
        }

        //获取已经创建单据的记录
        $scope.LoadCreatedERPDoc = function() {
            if ($scope.IfCreatedERPDoc) {
                $scope.All = false;
                $scope.NotCreatedERPDoc = false;
                $scope.$digest();

                LoadData();
            }
            else{
            }
        }

        //获取未创建单据的记录
        $scope.LoadNotCreateERPDoc = function() {
            if ($scope.NotCreatedERPDoc) {
                $scope.All = false;
                $scope.IfCreatedERPDoc = false;
                $scope.$digest();

                LoadData();
            }
            else{
            }
        }

        function LoadData(){
            var condit = {};
            if ($scope.IfCreatedERPDoc == true) {
                condit['IsCreatedERPDoc'] = 1
            } else if ($scope.NotCreatedERPDoc == true) {
                condit['IsCreatedERPDoc'] = 0
            };

            $scope.Module.PendingBusiness.Condition=condit;
            $scope.Module.PendingBusiness.load();
        };

        $scope.CreateDoc = function(){
            var selectedRecords = $scope.Module.PendingBusiness.SelectedRecords;

            // $resource('/api/barcodeinfo/CreateErpDoc').save(
            //     selectedRecords, 
            //     function (res) {
            //         res = res.toJSON();
            //         if (res.Ifsuccess){
            //             BaseFunction.alert("同步成功", "生单成功");
            //         }
            //         else{
            //             BaseFunction.alert("同步失败", res.data);
            //         }
            //     }
            // );
        }

        $scope.rowChange = function(){
            var fouscRecord = $scope.Module.PendingBusiness.FocusedRecord;
            if (fouscRecord){
                $scope.Module.PendingBusinessDetail.Condition = {PendingBusiness: fouscRecord._id};
                $scope.Module.PendingBusinessDetail.load();
            }
        };
    }
])